package cn.itsource.pethome.org.controller;

import cn.itsource.pethome.basic.util.AjaxResult;
import cn.itsource.pethome.basic.util.PageList;
import cn.itsource.pethome.org.domain.Employee;
import cn.itsource.pethome.org.domain.Shop;
import cn.itsource.pethome.org.query.EmployeeQuery;
import cn.itsource.pethome.org.query.ShopQuery;
import cn.itsource.pethome.org.service.IEmployeeService;
import cn.itsource.pethome.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 商家入驻控制层
 */
@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private IShopService shopService;
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 商家入驻
     *
     * @param shop 实体对象
     * @return
     */
    @PostMapping("/settledIn")
    public AjaxResult settledIn(@RequestBody Shop shop) {

        try {
            //调用商家入驻方法
            shopService.settledIn(shop);
            //返回结果
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            //返回结果
            return new AjaxResult(false, "入驻失败！" + e.getMessage());
        }

    }


    /**
     * 根据条件查询所有员工
     *
     * @return
     */
    @PatchMapping
    public PageList<Shop> loadPageByQuery(@RequestBody ShopQuery query) {

        return shopService.loadPageByQuery(query);
    }

    /**
     * 商铺信息批量删除
     *
     * @param ids
     * @return
     */
    //@RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    @PatchMapping("/del")//批量删除用数组接收参数
    public AjaxResult delete(@RequestBody Long[] ids) {
        try {
            //调用批量删除方法
            //删除商店信息
            shopService.patchDelete(ids);
            //删除用户信息
            employeeService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败!网络繁忙！" + e.getMessage());
        }
    }

    /**
     * 商店信息删除
     * @param id
     * @return
     */
    //@RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    @PostMapping("{id}")
    public AjaxResult DisableAudit(@PathVariable("id")Long id){
        try {
            //禁用
            shopService.DisableAudit(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败" +e.getMessage());
        }
    }
    /**
     * 商店信息审核通过
     * @param id
     * @return
     */
    //@RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    @PostMapping("/pass/{id}")
    public AjaxResult AuditPass(@PathVariable("id")Long id){
        try {
            //审核通过
            shopService.AuditPass(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败" +e.getMessage());
        }
    }
    /**
     * 商店信息审核不通过
     * @param id
     * @return
     */
    //@RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    @PostMapping("/noPass/{id}")
    public AjaxResult AuditNoPass(@PathVariable("id")Long id){
        try {
            //审核不通过
            shopService.AuditNoPass(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败" +e.getMessage());
        }
    }
    /**
     * 商店信息审核状态
     * @param id
     * @return
     */
    //@RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    @PatchMapping("/view/{id}")
    public Shop ReviewBusiness(@PathVariable("id")Long id){
        //返回审核状态
        return shopService.shopState(id);

    }
    /**
     * 商店信息进行回显
     * @param id
     * @return
     */
    //@RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    @PatchMapping("/loadById/{id}")
    public Shop loadByIdShop(@PathVariable("id")Long id){
        //返回审核状态
        return shopService.loadById(id);

    }

}